package com.jiu345.api.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiu345.api.mapper.*;
import com.jiu345.api.pojo.*;
import com.jiu345.api.pojo.bo.GoodsBo;
import com.jiu345.api.pojo.vo.GoodsVo;
import com.jiu345.api.pojo.vo.GoodsItemVo;
import com.jiu345.api.pojo.vo.SpecsVo;
import com.jiu345.core.page.PageQuery;
import com.jiu345.core.page.TableDataInfo;
import com.jiu345.api.service.IGoodsService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * package:IGoodsCateServiceImpl
 *
 * @author MrLei QQ:235921049
 * @date 2024-05-21
 * desc:商品
 */
@RequiredArgsConstructor
@Service
public class IGoodsServiceImpl implements IGoodsService {
    private final GoodsMapper baseMapper;
    private final GoodsCateMapper cateMapper;
    private final GoodsSkusMapper skusMapper;
    private final GoodsDetailsMapper detailsMapper;
    private final SkusSpecMapper specMapper;
    private final SkusSpecValueMapper specValueMapper;

    /*商品列表*/
    @Override
    public TableDataInfo<Goods> queryPageList(GoodsBo bo, PageQuery pageQuery) {
        Page<Goods> result = baseMapper.queryPageList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /*TODO 根据SKU查询所有商品，暂时不调用此方法*/
    @Override
    public TableDataInfo<GoodsSkus> querySkuPageList(GoodsBo bo, PageQuery pageQuery) {
        Page<GoodsSkus> result = skusMapper.querySkuPageList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /*商品详情*/
    @Override
    public GoodsVo getDetail(Long id) {
        Goods goods = baseMapper.selectById(id);
        //把商品主图转为轮播图数组输出
        String[] split = goods.getImgUrl().split(",");
        List<String> imgList = Arrays.asList(split);
        //创建返回的商品基本信息对象
        GoodsVo goodsVo = GoodsVo.builder()
                .imageUrl(imgList)
            .id(goods.getId())
            .name(goods.getName())
            .subName(goods.getSubName())
            .price(goods.getPrice())
            .oldPrice(goods.getOldPrice())
            .sale(goods.getSale())
            .collect(goods.getCollect())
            .createTime(goods.getCreateTime())
            .build();

        // 查询商品三级分类信息
        LambdaQueryWrapper<GoodsCategory> threeWrapper = new LambdaQueryWrapper<>();
        threeWrapper.eq(GoodsCategory::getId, goods.getCate3Id());
        GoodsCategory three = cateMapper.selectOne(threeWrapper);
        if (three != null) {
            //设置第三级信息
            goodsVo.setCategory3Id(three.getId());
            goodsVo.setCategory3Name(three.getName());
            // 查询二级分类信息
            LambdaQueryWrapper<GoodsCategory> twoWrapper = new LambdaQueryWrapper<>();
            twoWrapper.eq(GoodsCategory::getId, three.getParentId());
            GoodsCategory two = cateMapper.selectOne(twoWrapper);
            if (two != null) {
                goodsVo.setCategory2Id(two.getId());
                goodsVo.setCategory2Name(two.getName());
                // 查询顶级分类信息
                LambdaQueryWrapper<GoodsCategory> oneWrapper = new LambdaQueryWrapper<>();
                oneWrapper.eq(GoodsCategory::getId, two.getParentId());
                GoodsCategory one = cateMapper.selectOne(oneWrapper);
                if (one != null) {
                    goodsVo.setCategory1Id(one.getId());
                    goodsVo.setCategory1Name(one.getName());
                }
            }
        }
        //查询sku列表
        LambdaQueryWrapper<GoodsSkus> skuWrapper =  new LambdaQueryWrapper<>();
        skuWrapper.eq(GoodsSkus::getGoodsId, id);
        List<GoodsSkus> skusList = skusMapper.selectList(skuWrapper);
        //查询sku里面的spec
        if(skusList != null){
            for(GoodsSkus sku : skusList){
                //把attributes的值是[{name:'口味',valueName:'香辣味'},{name:'包装',valueName:'60克'}]转数组
                List<HashMap<String, String>> list = JSON.parseObject(sku.getAttributes(), new TypeReference<>(){});
                sku.setSpecs(list);
            }
        }
        //设置SKU列表
        goodsVo.setSkus(skusList);
        //查询spec表
        LambdaQueryWrapper<SkusSpec> specWrapper = new LambdaQueryWrapper<>();
        specWrapper.eq(SkusSpec::getGoodsId,id);
        List<SkusSpec> specList = specMapper.selectList(specWrapper);
        //查询specList里面的关联value值
        for(SkusSpec spec:specList){
            LambdaQueryWrapper<SkusSpecValue> specValueWrapper = new LambdaQueryWrapper<>();
            specValueWrapper.eq(SkusSpecValue::getSpecId,spec.getId());
            List<SkusSpecValue> specValueList = specValueMapper.selectList(specValueWrapper);
            spec.setSpecValue(specValueList);
        }
        goodsVo.setSpecs(specList);
        //根据id=mall_goods_details的goodsId查询商品详情
        LambdaQueryWrapper<GoodsDetails> detailWrapper = new LambdaQueryWrapper<>();
        detailWrapper.eq(GoodsDetails::getGoodsId, goods.getId());
        GoodsDetails goodsDetails = detailsMapper.selectOne(detailWrapper);
        goodsVo.setGoodsDetails(goodsDetails);
        return goodsVo;
    }

    /*TODO 首页热销商品，本套系统暂时不用此方法*/
    @Override
    public List<GoodsSkus> selectSkuBy(GoodsSkus bo) {
        return skusMapper.selectSkuBy(bo);
    }


    /* TODO 根据SKU查询商品详情,本套系统暂时不用此方法*/
    @Override
    public GoodsItemVo getSku(Long skuId) {
        //创建VO对象
        GoodsItemVo goodsItemVo = new GoodsItemVo();
        //查询SKU
        GoodsSkus goodsSkus = skusMapper.selectById(skuId);
        goodsItemVo.setGoodsSkus(goodsSkus);
        //根据sku的goodsId查询主商品
        Goods goods = baseMapper.selectById(goodsSkus.getGoodsId());
        goodsItemVo.setGoods(goods);
        //TODO 根据商品goods查询所属的sku列表,这个暂时不使用
        Map<String, Object> skusSpecValueMap = new HashMap<>();
        LambdaQueryWrapper<GoodsSkus> skuWrapper = new LambdaQueryWrapper<>();
        skuWrapper.eq(GoodsSkus::getGoodsId, goods.getId());
        List<GoodsSkus> skusList = skusMapper.selectList(skuWrapper);
        skusList.forEach(item -> {
            //根据specId查询mall_goods_spec表信息
            LambdaQueryWrapper<SkusSpec> skusSpecWrapper = new LambdaQueryWrapper<>();
            //sku表specId=spec表的主键id
            skusSpecWrapper.eq(SkusSpec::getId, item.getAttributes());
            SkusSpec spec = specMapper.selectOne(skusSpecWrapper);
            //设置到sku
            SpecsVo specs = new SpecsVo();
            specs.setName(spec.getName());
            //查询spec对应的val表
            LambdaQueryWrapper<SkusSpecValue> skusSpecValueWrapper = new LambdaQueryWrapper<>();
            skusSpecValueWrapper.eq(SkusSpecValue::getSpecId, spec.getId());
            SkusSpecValue valueList = specValueMapper.selectOne(skusSpecValueWrapper);
            specs.setValueName(valueList.getName());
//            item.setAttributes(specs);
        });
        //根据goods查询商品详情
        LambdaQueryWrapper<GoodsDetails> detailWrapper = new LambdaQueryWrapper<>();
        detailWrapper.eq(GoodsDetails::getGoodsId, goods.getId());
        GoodsDetails goodsDetails = detailsMapper.selectOne(detailWrapper);
        goodsItemVo.setGoodsDetails(goodsDetails);
        //根据sku查询轮播图,将字符串,间打散
        String[] split = goodsSkus.getImgUrl().split(",");
        List<String> list = Arrays.asList(split);
//        goodsItemVo.setGoodsImage(list);
        return goodsItemVo;
    }


    /*首页一级分类下数据*/
    @Override
    public List<Goods> selectByCate1Id(Long id) {
        LambdaQueryWrapper<Goods> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Goods::getCate1Id, id);
        return baseMapper.selectList(lqw);
    }
}
